package com.virtium.vpm.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * Provides Date utility methods that can be used to perform common operations.
 * @author TuanAnh
 */
public class DateUtil {

	protected static Logger log = Logger.getLogger(DateUtil.class);
	
	public static final String DB_DATE_FORMAT_ddMMyyyyHHmmss = "ddMMyyyyHHmmss";
	
    public DateUtil() {
    }
    
    /**
     * Gets the current date.
     *
     * @param pattern
     * @return
     * @throws Exception
     */
    public static String getCurrentDate(String pattern) throws Exception {

        try {
            Date currentDate = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            return formatter.format(currentDate);

        } catch (Exception ex) {
            log.error("Exception occurred while retrieving the current date" + 
            		ex.getMessage());
            throw new Exception(ex.toString());
        }
    }

    /**
     * Formats the date for display
     *
     * @param inputFormat
     * @param outputFormat
     * @param dateToFormat
     * @return
     * @throws Exception
     */
    public static String getDateFormat(String inputFormat,
                                       String outputFormat,
                                       String dateToFormat)
            throws Exception {

        SimpleDateFormat sdfInput = new SimpleDateFormat(inputFormat);
        SimpleDateFormat sdfOutput = new SimpleDateFormat(outputFormat);

        Date date = sdfInput.parse(dateToFormat);

        return (sdfOutput.format(date)).toString();
    }
    
    public static String dateToStr(Date date, String outputFormat)
    {
    	if (outputFormat == null || outputFormat.length() == 0)
    		outputFormat = "MM/dd/yyyy";    	
    	return dateTimeToStr(date, outputFormat);
    }
    public static String dateTimeToStr(Date date, String outputFormat)
    {
    	if (outputFormat == null || outputFormat.length() == 0)
    		outputFormat = "MM/dd/yyyy";
    	SimpleDateFormat sdfOutput = new SimpleDateFormat(outputFormat);
    	return (sdfOutput.format(date)).toString();
    }
    
    /**
     * Converts String to Date.
     * @param dateToConvert
     * @param pattern
     * @return
     */
    public static java.util.Date convertDate (String dateToConvert, String pattern) {
        java.util.Date formattedDate = null;
        try {
            SimpleDateFormat sdfInput = new SimpleDateFormat(pattern);
            formattedDate = sdfInput.parse(dateToConvert);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return formattedDate;
    }
    
    /**
     * Checks if From date is greater than To date.
     * Returns true if From date is greater than To date.
     * Else false.
     *
     * @param dateFrom
     * @param dateTo
     * @param pattern
     * @return
     */
    public static boolean checkFutureDate(String dateFrom, String dateTo, String pattern) {

        try {
            SimpleDateFormat simpleDate = new SimpleDateFormat(pattern);
            Date checkFrom = simpleDate.parse(dateFrom);
            Date checkTo = simpleDate.parse(dateTo);

            // compare dates
            return checkFrom.after(checkTo);
        } catch (Exception ex) {
            log.error(ex);
            return false;
        }
    }    
    
    
    public static String getCurrentYear() {
    	Calendar rightNow = Calendar.getInstance();
		return Integer.toString(rightNow.get(Calendar.YEAR));
    }
}